package com.wanmait.schedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.schedule.entity.Notice;
import com.wanmait.schedule.entity.NoticeRecord;
import com.wanmait.schedule.entity.Student;
import com.wanmait.schedule.entity.Teacher;
import com.wanmait.schedule.interceptor.WebSocketServer;
import com.wanmait.schedule.mapper.*;
import com.wanmait.schedule.service.NoticeRecordService;
import com.wanmait.schedule.service.NoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.schedule.service.StudentService;
import com.wanmait.schedule.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 通知 服务实现类
 * </p>
 *
 * @author wanmait
 * @since 2022-12-05
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Resource
    private NoticeMapper noticeMapper;

    @Autowired
    private NoticeRecordMapper noticeRecordMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private WebSocketServer webSocketServer;

    @Override
    public PageInfo<Notice> findAll(Notice notice,Integer pageNum) {
        if(pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,5);

        List<Notice> noticeList = noticeMapper.findAll(notice);
        PageInfo<Notice> pageInfo = new PageInfo<>(noticeList,11);

        return pageInfo;
    }

    @Override
    public Boolean save(Map map) {
        System.out.println(map);
        //学生id集合
        List<Integer> student = null;
        //教师id集合
        List<Integer> teacher = null;
        //全部教师
        String allTeacher = null;
        //全部学生
        String allStudent = null;
        //全选
        String all = null;
        //通知总人数
        Integer noticeCount = 0;
        //通知内容
        Notice notice = new Notice();

        notice.setInputId(1);

        if (((List<Integer>)map.get("students")).size()!=0){
            student = (List<Integer>) map.get("students");
        }
        if (((List<Integer>)map.get("teachers")).size()!=0){
            teacher = (List<Integer>) map.get("teachers");
        }
        if ((String)map.get("allStudent")!=null&&(String)map.get("allStudent")!=""){
            allStudent = (String)map.get("allStudent");
        }
        if ((String)map.get("allTeacher")!=null&&(String)map.get("allTeacher")!=""){
            allTeacher = (String)map.get("allTeacher");
        }
        if ((String)map.get("all")!=null&&(String)map.get("all")!=""){
            all = (String)map.get("all");
        }

        //获得map中的notice属性
        Set<Map.Entry<String,Object>> entrySet = ((Map) map.get("notice")).entrySet();
        for (Map.Entry<String,Object> entry:entrySet){
            switch (entry.getKey()){
                case "levelId":
                    notice.setLevelId((Integer) entry.getValue());
                    break;
                case "typeId":
                    notice.setTypeId((Integer) entry.getValue());
                    break;
                case "title":
                    notice.setTitle((String) entry.getValue());
                    break;
                case "content":
                    notice.setContent((String) entry.getValue());
                    break;
                case "publish":
                    notice.setPublish((Boolean) entry.getValue());
                    break;
                default:
                    break;
            }
        }
        if(allTeacher!=null){
            teacher = teacherMapper.findAllId();
        }
        if(allStudent!=null){
            student = studentMapper.findByAll();
        }
        if(all!=null){
            teacher = teacherMapper.findAllId();
            student = studentMapper.findByAll();
        }

        if(student!=null){
            //将学生于班级中的学生放在一起
            noticeCount += student.size();
        }
        if(teacher!=null){
            noticeCount += teacher.size();
        }
        //将通知数量添加到通知
        notice.setNoticeNumber(noticeCount);
        noticeMapper.insert(notice);

        if(student!=null){
            noticeRecordMapper.insertStudent(student,notice.getId());

            List<String> studentList = new ArrayList<>();
            for (Integer item : student) {
                studentList.add("student:"+item);
            }
            try {
                webSocketServer.sendMessage(notice.getTitle(),studentList);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        if(teacher!=null){
            noticeRecordMapper.insertTeacher(teacher,notice.getId());

            List<String> teacherList = new ArrayList<>();
            for (Integer item : teacher) {
                teacherList.add("teacher:"+item);
            }
            try {
                webSocketServer.sendMessage(notice.getTitle(),teacherList);
            } catch (IOException e) {
                e.printStackTrace();
            }


        }









        return notice.getId()!=null;
    }

    @Override
    public PageInfo<Teacher> findIdByTeacher(Integer id, Integer pageNum) {
        if(pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,5);
        List<Teacher> teacherList = noticeRecordMapper.findIdByTeacher(id);
        PageInfo<Teacher> pageInfo = new PageInfo<>(teacherList,11);
        return pageInfo;
    }

    @Override
    public PageInfo<Student> findIdByStudent(Integer id, Integer pageNum) {
        if(pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,5);
        List<Student> studentList = noticeRecordMapper.findIdByStudent(id);
        PageInfo<Student> pageInfo = new PageInfo<>(studentList,11);
        return pageInfo;
    }

    @Override
    public Boolean delNoticeStudent(NoticeRecord noticeRecord) {
        int delete = noticeRecordMapper.delNoticeStudent(noticeRecord);
        return delete>0;
    }

    @Override
    public Boolean delNoticeTeacher(NoticeRecord noticeRecord) {
        int delete = noticeRecordMapper.delNoticeTeacher(noticeRecord);
        return delete>0;
    }

    @Override
    public List<Notice> notices() {
        return noticeMapper.notices();
    }

    @Override
    public Integer readNum(Integer id) {
        return noticeMapper.readNum(id);
    }
}
